package com.itjiange.springbootinit.mq;

import com.rabbitmq.client.*;

public class DirectConsumer {

    private static final String EXCHANGE_NAME = "direct-exchange";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("154.8.205.195");
        factory.setUsername("itjiange");
        factory.setPassword("123456");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);

        // 创建并绑定消息队列
        channel.queueDeclare("yijian-queue", true, false, false, null);
        channel.queueBind("yijian-queue", EXCHANGE_NAME, "yijian");

        // 创建并绑定消息队列
        channel.queueDeclare("erjian-queue", true, false, false, null);
        channel.queueBind("erjian-queue", EXCHANGE_NAME, "erjian");

        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        DeliverCallback deliverCallback1 = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println(" [yijian] Received '" +
                    delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
        };
        channel.basicConsume("yijian-queue", true, deliverCallback1, consumerTag -> {
        });

        DeliverCallback deliverCallback2 = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println(" [erjian] Received '" +
                    delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
        };
        channel.basicConsume("erjian-queue", true, deliverCallback2, consumerTag -> {
        });
    }
}